package com.atguigu.yygh.orders.controller;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.util.TokenUtils;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.hosp.client.HospitalFeignClient;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.orders.service.OrderInfoService;
import com.atguigu.yygh.user.client.PatientFeignClient;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author ztz
 * @since 2022-03-17
 */
//创建controller方法
@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;


    @ApiOperation(value = "创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public R submitOrder(
            @ApiParam(name = "scheduleId", value = "排班id", required = true)
            @PathVariable String scheduleId,
            @ApiParam(name = "patientId", value = "就诊人id", required = true)
            @PathVariable Long patientId) {

        Long orderId = orderInfoService.saveOrder(scheduleId, patientId);
        return R.ok().data("orderId", orderId);
    }

    @ApiOperation(value = "查看订单详情")
    @GetMapping("auth/getStatusList")
    public R getStatusList(HttpServletRequest request) {
        // 获取用户的id
        Long userId = TokenUtils.getUserId(request);
        List<Map<String, Object>> statusList = OrderStatusEnum.getStatusList();
        return R.ok().data("statusList", statusList);
    }

    @ApiOperation("根据条件查询，分页")
    @GetMapping("auth/{page}/{limit}")
    public R getPageList(@PathVariable("page") Integer page,
                         @PathVariable("limit") Integer limit,
                         OrderQueryVo orderQueryVo, HttpServletRequest request) {
        Long userId = TokenUtils.getUserId(request);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> iPage = new Page<>(page, limit);
        Page<OrderInfo> infoPage = orderInfoService.getPageList(iPage, orderQueryVo);

        return R.ok().data("pageModel", infoPage);
    }

    @ApiOperation("根据订单id查询数据详情")
    @GetMapping("auth/getOrders/{orderId}")
    public R getOrders(@PathVariable("orderId") Integer orderId) {
        OrderInfo orderInfo = orderInfoService.getOrders(orderId);
        return R.ok().data("orderInfo", orderInfo);
    }

    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public R cancelOrder(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @PathVariable("orderId") Long orderId) {

        Boolean flag = orderInfoService.cancelOrder(orderId);
        return R.ok().data("flag",flag);
    }

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderInfoService.getCountMap(orderCountQueryVo);
    }

}

